﻿using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RrslLite.Net.Attributes;
using RrslLite.Net.Messages;
using RrslLite.Net.Proxies;

namespace App.Test.RrslLiteTEST
{
    /// <summary>
    /// Summary description for AsyncMessageProcessorProxyTEST
    /// </summary>
    [TestClass]
    public class AsyncMessageProcessorProxyTest
    {
        [TestInitialize]
        public void Init()
        {
            KnownTypeProvider.RegisterModelDomainNamespace("App.Business.Entities");
        }

        private static AsyncMessageProcessorProxy GetAsyncMessageProcessorProxy()
        {
            return new AsyncMessageProcessorProxy("basicHttp_IWcfMessageService");
        }

        private static MessageProcessorProxy GetSyncMessageProcessorProxy()
        {
            return new MessageProcessorProxy("basicHttp_IWcfMessageService");
        }

        [TestMethod]
        public void SyncMessageProcessorProxy_InvokeOperations()
        {
            var testee = GetSyncMessageProcessorProxy();

            var messages = new Message[]
                               {
                                   new Message(Guid.NewGuid())
                                       {
                                           OperationContainerName = "IPersonFacade",
                                           OperationName = "GetActivePersonsFor",
                                           OperationParameters = new Dictionary<string,object>() { { "country", "CZ" } }
                                       },
                                   new Message(Guid.NewGuid())
                                       {
                                           OperationContainerName = "ICountryFacade",
                                           OperationName = "GetByCode",
                                           OperationParameters = new Dictionary<string,object>() { { "country", "CZ" } }
                                       }
                               };

            var result = testee.InvokeOperations(messages);
			Assert.IsNotNull(result);
			Assert.IsTrue(result.Length > 0);


			foreach (var message in result)
			{
				Assert.IsFalse(message.ContainsException);
			}

        }

        [TestMethod]
		[Timeout(10000)]
        public void AsyncMessageProcessorProxy_InvokeOperationsAsync()
        {
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);

            var testee = GetAsyncMessageProcessorProxy();

            var messages = new Message[]
                               {
                                   new Message(Guid.NewGuid())
                                       {
                                           OperationContainerName = "IPersonFacade",
                                           OperationName = "GetActivePersonsFor",
                                           OperationParameters = new Dictionary<string,object>() { { "country", "CZ" } }
                                       },
                                   new Message(Guid.NewGuid())
                                       {
                                           OperationContainerName = "ICountryFacade",
                                           OperationName = "GetByCode",
                                           OperationParameters = new Dictionary<string,object>() { { "country", "CZ" } }
                                       }
                               };
            testee.InvokeOperationsAsync(
                messages,
                e =>
                    {
						Assert.IsNotNull(e.Result);
						Assert.IsTrue(e.Result.Length > 0);


						foreach (var message in e.Result)
						{
							Assert.IsFalse(message.ContainsException);
						}
                    	manualResetEvent.Set();
                    }
                );

            manualResetEvent.WaitOne();
        }
    }
}
